package com.mdb.open.epay.facade.epay;

import com.alibaba.fastjson.JSON;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.open.epay.domain.bo.GetReconFileBo;
import com.mdb.open.epay.domain.po.epay.EpReconCustFile;
import com.mdb.open.epay.domain.vo.epay.ReconFileVo;
import com.mdb.open.epay.service.epay.EpReconCustFileService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 對賬文件相關
 *
 * @author liuyi
 * @date 2019/11/14
 **/
@Log4j2
@Service
public class ReconFacade {

    @Autowired
    private EpReconCustFileService epReconCustFileService;

    /**
     * 对账文件查询
     */
    public ResultEx<ReconFileVo> getReconFile(GetReconFileBo bo) {
        LocalDateTime reconTimeBegin = bo.getReconDate().atStartOfDay();
        LocalDateTime reconTimeEnd = bo.getReconDate().plusDays(1).atStartOfDay();
        EpReconCustFile reconFile = epReconCustFileService.getReconFile(bo.getCustomerCode(), reconTimeBegin, reconTimeEnd, bo.getReconType());
        if (ObjectUtil.isEmpty(reconFile)) {
            return ResultEx.fail("無當日對賬文件");//TODO 無當日對賬文件
        }
        log.error("解析文件：{}", JSON.toJSONString(reconFile));
        ReconFileVo reconFileVo = new ReconFileVo();
        reconFileVo.setReconDate(bo.getReconDate());
        reconFileVo.setFileNo(reconFile.getFileNo());
        return ResultEx.ok(reconFileVo);
    }
}
